package com.example.administrator.szb.activity.myyewu;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BaseActivity;
import com.example.administrator.szb.adapter.BaseAdapter;
import com.example.administrator.szb.bean.BusinessBean;
import com.example.administrator.szb.bean.ResultBean;
import com.example.administrator.szb.bean.YWBQGLBean;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.view.TipsPop;

import java.util.HashMap;
import java.util.Iterator;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by D N on 2017/11/8.
 */

public class YWGLNOActivity_new extends BaseActivity {
    @Bind(R.id.ywgl_no_new_recyclerview)
    RecyclerView ywglNoNewRecyclerview;
    @Bind(R.id.ywgl_no_new_button_tj)
    Button ywglNoNewButtonTj;

    public static BusinessBean businessBean_yw;
    public YWBQGLBean ywbqBean;

    BaseAdapter<BusinessBean.DataBean> adapter;
    HashMap<Integer, Integer> info = new HashMap<Integer, Integer>() {
        @Override
        public String toString() {
            String info_id = "";
            Iterator<Integer> it = info.keySet().iterator();
            while (it.hasNext()) {
                info_id = info_id + info.get(it.next()) + ",";
            }
            return info_id;
        }
    };
    @Bind(R.id.ywgl_no_text_show)
    TextView ywglNoTextShow;
    @Bind(R.id.ywgl_no_text_show_ll)
    LinearLayout ywgl_no_text_show_ll;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ywgl_no_new_activity);
        ButterKnife.bind(this);
        //businessBean_yw = WecomeActivity.businessBean;
        //businessBean_yw.getData().get(0).setIsChoose(1);
        initToolbar(R.id.ywgl_no_new_toolbar, "业务管理");


        try {
            if (YWGLYESActivity.ywbqglBean.getData().size() > 0) {
                ywglNoTextShow.setVisibility(View.GONE);
                ywgl_no_text_show_ll.setVisibility(View.GONE);
            }
        }catch (Exception e){

        }

        requestData();


    }

    private void requestData() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        map.put("business_id", "" + 0);

        String urls = URLAddress.URLBASE + URLAddress.URL_BUSINESS2;
        HttpUtils.post2(activity,urls, map, 0, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                str = str.replace("false","\"\"");
                businessBean_yw = JSON.parseObject(str,new TypeReference<BusinessBean>(){});
                if (businessBean_yw.getResult() == 1){
                    initView();
                }
            }

            @Override
            public void onFailure(int requestCode, String str) {

            }
        });
        /*HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, BusinessBean.class, , map, new Response.Listener() {
            @Override
            public void onResponse(Object response) {


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });*/
    }

    private void initView() {
        /**
         * 将已选择过的标签显示出来
         */
        if (YWGLYESActivity.ywbqglBean != null && YWGLYESActivity.ywbqglBean.getData().size() >0){
            ywbqBean = YWGLYESActivity.ywbqglBean;
            for (int i = 0; i < ywbqBean.getData().size(); i++) {
                int id = ywbqBean.getData().get(i).getId();

                for (int j = 0; j < businessBean_yw.getData().size(); j++) {
                    if (id == businessBean_yw.getData().get(j).getId()){
                        businessBean_yw.getData().get(j).setIsChoose(1);
                        break;
                    }else{
                        //businessBean_yw.getData().get(j).setIsChoose(0);
                    }
                }
            }
        }else{
            for (int j = 0; j < businessBean_yw.getData().size(); j++) {
                businessBean_yw.getData().get(j).setIsChoose(0);
            }
        }
        initRecyclerView();
    }


    private void initRecyclerView() {
        ywglNoNewRecyclerview.setLayoutManager(new LinearLayoutManager(this));
        adapter = new BaseAdapter<BusinessBean.DataBean>(this, businessBean_yw.getData()) {
            @Override
            public void convert(final int position, BaseViewHolder holder, final BusinessBean.DataBean dataBean) {
                final LinearLayout imageView = (LinearLayout) holder.getView(R.id.wenhao);
                final String strs = businessBean_yw.getData().get(position).getBusiness_desc();
                imageView.setVisibility(View.GONE);
                if (!TextUtils.isEmpty(strs)){
                    imageView.setVisibility(View.VISIBLE);
                }

                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        TipsPop.show(YWGLNOActivity_new.this,imageView,strs);
                    }
                });
                if (dataBean.getType() == 2) {
                    ((TextView) holder.getView(R.id.ywgl_no_new_text)).setText(dataBean.getTitle());
                    if (dataBean.getIsChoose() == 1) {
                        (holder.getView(R.id.ywgl_no_new_text_gpzy)).setVisibility(View.VISIBLE);
                    } else {
                        (holder.getView(R.id.ywgl_no_new_text_gpzy)).setVisibility(View.INVISIBLE);
                    }


                } else if (dataBean.getType() == 1) {



                    if (position == businessBean_yw.getData().size() - 4) {
                        ((TextView) holder.getView(R.id.ywgl_no_new_text_three)).setText(dataBean.getTitle());

                        if (dataBean.getIsChoose() == 1){
                            ((CheckBox) holder.getView(R.id.ywgl_no_new_checkbox_fhbs_three)).setChecked(true);
                            info.put(position, dataBean.getId());
                        }else {
                            ((CheckBox) holder.getView(R.id.ywgl_no_new_checkbox_fhbs_three)).setChecked(false);
                        }

                        ((CheckBox) holder.getView(R.id.ywgl_no_new_checkbox_fhbs_three)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                                if (b) {
                                    info.put(position, dataBean.getId());
                                } else {
                                    info.remove(position);
                                }
                            }
                        });
                    } else {
                        ((TextView) holder.getView(R.id.ywgl_no_new_text_one)).setText(dataBean.getTitle());

                        if (dataBean.getIsChoose() == 1){
                            ((CheckBox) holder.getView(R.id.ywgl_no_new_checkbox_fhbs_one)).setChecked(true);
                            info.put(position, dataBean.getId());
                        }else {
                            ((CheckBox) holder.getView(R.id.ywgl_no_new_checkbox_fhbs_one)).setChecked(false);
                        }
                        ((CheckBox) holder.getView(R.id.ywgl_no_new_checkbox_fhbs_one)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                            @Override
                            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                                if (b) {
                                    info.put(position, dataBean.getId());
                                } else {
                                    info.remove(position);
                                }
                            }
                        });
                    }


                }
            }

            @Override
            public int getLayoutId(int viewType) {
                if (businessBean_yw.getData().get(viewType).getType() == 1) {

                    if (viewType == businessBean_yw.getData().size() - 4) {
                        return R.layout.view_ywgl_no_new_three_item;
                    }

                    return R.layout.view_ywgl_no_new_one_item;
                } else {
                    return R.layout.view_ywgl_no_new_two_item;
                }

            }
        };

        adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (businessBean_yw.getData().get(position).getType() == 2) {


                    if(businessBean_yw.getData().get(position).getIsChoose() == 1 && businessBean_yw.getData().get(position).getGenre() == 0){
                       // Toasts.show(YWGLNOActivity_new.this, "it will go to new activity.", Toast.LENGTH_SHORT);
                        Intent intent_new =new Intent(YWGLNOActivity_new.this,YWXQActivity_save.class);
                        String title = businessBean_yw.getData().get(position).getTitle();
                        int id = businessBean_yw.getData().get(position).getId();
                        intent_new.putExtra("id", id);
                        intent_new.putExtra("title", title);
                        intent_new.putExtra("position", position);
                        startActivity(intent_new);

                    }
                    else if(businessBean_yw.getData().get(position).getIsChoose() == 0 && businessBean_yw.getData().get(position).getGenre() == 0){
                        Intent intent = new Intent(YWGLNOActivity_new.this, YWXQActivity.class);
                        int id = businessBean_yw.getData().get(position).getId();
                        String title = businessBean_yw.getData().get(position).getTitle();
                        intent.putExtra("id", id);
                        intent.putExtra("title", title);
                        intent.putExtra("position", position);
                        startActivity(intent);
                    }
                    else if(businessBean_yw.getData().get(position).getIsChoose() == 0 && businessBean_yw.getData().get(position).getGenre() == 1){
                        Intent intent = new Intent(YWGLNOActivity_new.this, YWXQActivity_bgzr.class);
                        int id = businessBean_yw.getData().get(position).getId();
                        String title = businessBean_yw.getData().get(position).getTitle();
                        intent.putExtra("id", id);
                        intent.putExtra("title", title);
                        intent.putExtra("position", position);
                        startActivity(intent);
                    }
                    else if(businessBean_yw.getData().get(position).getIsChoose() == 1 && businessBean_yw.getData().get(position).getGenre() == 1){
                        Intent intent_new =new Intent(YWGLNOActivity_new.this,YWXQActivity_save_bgzr.class);
                        String title = businessBean_yw.getData().get(position).getTitle();
                        int id = businessBean_yw.getData().get(position).getId();
                        intent_new.putExtra("id", id);
                        intent_new.putExtra("title", title);
                        intent_new.putExtra("position", position);
                        startActivity(intent_new);
                    }
                }


            }
        });
        ywglNoNewRecyclerview.setAdapter(adapter);
    }

    @OnClick(R.id.ywgl_no_new_button_tj)
    public void onViewClicked() {
        String id_info = info.toString();
        if (!TextUtils.isEmpty(id_info)) {
            HashMap<String, String> map = new HashMap<>();
            map.put("code", HttpUtil.getParam());
            map.put("users_id", SampleApplicationLike.getUserloginInstance().getId() + "");
            map.put("business_id", id_info);
            map.put("describe", "");


            HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, ResultBean.class, URLAddress.URLBASE + URLAddress.URL_YWBQSC, map, new Response.Listener() {
                @Override
                public void onResponse(Object response) {
                    ResultBean bean = (ResultBean) response;
                    if (bean.getResult() == 1) {
                        YWGLYESActivity.canRefresh = true;
                        if (!YWGLYESActivity.isLoad){
                            startActivity(new Intent(YWGLNOActivity_new.this, YWGLYESActivity.class));
                        }

                        finish();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

        } else {
            //startActivity(new Intent(YWGLNOActivity_new.this, YWGLYESActivity.class));
            ywbqBean = null;
            businessBean_yw = null;
            finish();
        }
    }

    @Override
    protected void onRestart() {
        adapter.notifyDataSetChanged();
        super.onRestart();
    }
}
